﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace Macaw.Wss3.FieldContentPanelSolution
{
    /// <summary>
    /// Based on: http://www.andrewconnell.com/blog/archive/2009/07/17/Hiding-Empty-Content-Areas-in-SharePoint-2007-WCM-Publishing-Sites.aspx
    /// </summary>
    public class FieldContentPlaceholder : PlaceHolder
    {
        #region Private Fields
        private bool _AlwaysShowInEditMode = true;
        #endregion

        #region Public properties
        /// <summary>
        /// This property will determine which Field of the current listitem will be checked for being empty.
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// This property will determine if the contents of the Panel always will be shown in EditMode, no matter if the field is empty.
        /// </summary>
        /// <remarks>The default value is "True"</remarks>
        public bool AlwaysShowInEditMode 
        {
            get { return _AlwaysShowInEditMode; }
            set { _AlwaysShowInEditMode = value;}
        }
        #endregion

        /// <summary>
        /// This method will check if the given field with the fieldvalue is "empty"
        /// </summary>
        /// <param name="field">The field-definition</param>
        /// <param name="fieldvalue">The fieldvalue</param>
        /// <returns>"True" if empty</returns>
        protected virtual bool FieldEmpty(SPField field, object fieldvalue)
        {
            return EmptyFieldValueHelper.FieldEmpty(field, fieldvalue);
        }

        /// <summary>
        /// This property will be queried to determine if the situation allows the panel to be hidden.
        /// </summary>
        protected virtual bool HideAllowed
        {
            get
            {
                if (SPContext.Current.FormContext != null && SPContext.Current.FormContext.FormMode != Microsoft.SharePoint.WebControls.SPControlMode.Display)
                {
                    if (AlwaysShowInEditMode) return false;
                }

                return true;
            }
        }

        protected override void CreateChildControls()
        {            
            if (SPContext.Current != null && SPContext.Current.ListItem != null && !string.IsNullOrEmpty(FieldName))
            {
                SPField field = SPContext.Current.ListItem.Fields.GetFieldByInternalName(FieldName);
                if (field != null)
                {
                    object fieldvalue = SPContext.Current.ListItem[field.Id];
                    if (FieldEmpty(field, fieldvalue) && HideAllowed)
                    {
                        Controls.Clear();
                        return;
                    }
                }
            }            

            base.CreateChildControls();
        }

    }
}
